﻿using Quartz;

namespace AVCJobDispatcherLinux.TaskJobEngine
{
    public class Trigger
    {
        public static ITrigger Builder(TriggerKey triggerKey, int seconds)
        {
            return TriggerBuilder.Create()
                .WithIdentity(triggerKey)
                .StartNow()
                .WithSimpleSchedule(x => x
                    .WithIntervalInSeconds(seconds)
                    .RepeatForever())
                .Build();
        }
        public static ITrigger BuilderRepeatCount(TriggerKey triggerKey, int interval, int times)
        {
            return TriggerBuilder.Create()
                .WithIdentity(triggerKey)
                .StartNow()
                .WithSimpleSchedule(x => x
                    .WithIntervalInSeconds(interval)
                    .WithRepeatCount(times))
                .Build();
        }
        public static ITrigger Builder(TriggerKey triggerKey, JobInfo entity)
        {
            if (entity.Second > 0)
            {
                return TriggerBuilder.Create()
                    .WithIdentity(triggerKey)
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInSeconds(entity.Second)
                        .RepeatForever())
                    .Build();
            }
            else
            {
                return TriggerBuilder.Create()
                    .WithIdentity(triggerKey)
                    .ForJob(JobTriggerConver.GetJobKey(entity))
                    .WithSchedule(CronScheduleBuilder.CronSchedule(entity.Cron)
                    .WithMisfireHandlingInstructionFireAndProceed())
                    .Build();
            }
        }
    }
}
